package com.tianzehua.thrift;

import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import thrift.generated.PersonService;

/**
 * thrift server bootstrap class
 * @author tianzehua
 * @date 2019/03/16
 */
public class ThriftSerever {

    public static void main(String[] args) throws  Exception{
        /* 建立的一个非阻塞的socket对象，绑定一个端口，来进行传输 */
        TNonblockingServerSocket socket = new TNonblockingServerSocket(8899);
        THsHaServer.Args arg = new THsHaServer.Args(socket).minWorkerThreads(2).maxWorkerThreads(4);
        /* 获取调用对象 */
        PersonService.Processor<PersonSericeImpl> processor = new PersonService.Processor<>(new PersonSericeImpl());
        /*  字节码压缩对象 */
        arg.protocolFactory(new TCompactProtocol.Factory());
        /*  传输层对象 */
        arg.transportFactory(new TFramedTransport.Factory());
        /*  协议层对象 强调的是数据的结构 */
        arg.processorFactory(new TProcessorFactory(processor));
        /* 半同步半异步服务器 */
        TServer server = new THsHaServer(arg);
        System.out.println("Thrift Server Started!");

        server.serve();
    }
}
